package com.wf.smartcomm.server.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.wf.smartcomm.server.bean.User;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-27 下午7:02:19 
 * 
 */
public class LoginInterceptor implements Interceptor{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		String requestType = request.getHeader("X-Requested-With");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		HttpSession session = request.getSession(false);
		if (session == null) {
			if (StringUtils.isNotEmpty(requestType)) {
				response.getWriter().print(201);
			}else{
				response.sendRedirect("/WisdomHome");
			}
			return null;
		}
		User user = (User) session.getAttribute("loginUser");
		if (user == null) {
			if (StringUtils.isNotEmpty(requestType)) {
				response.getWriter().print(201);
			}else {
				response.sendRedirect("/WisdomHome");
			}
			return null;
		}
		
		return invocation.invoke();
	}

}
